package io.renren.modules;

import org.apache.xmlgraphics.image.codec.util.SeekableOutputStream;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.thingsboard.rest.client.RestClient;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
import com.csvreader.CsvReader;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class TbClient {

    String url = "https://thingsboard.cmhijs.cn:8443";
    String username = "716_api@thingsboard.com";
    String password = "716Api@2024";

//    @Autowired
    Optional<User> user;

//    @Autowired
    RestClient client;

    public void login() {
        client = new RestClient(url);
        client.login(username, password);
        user = client.getUser();
    }

    //获取设备列表
    public void getDevicesLatestData() {


        PageData<Device> devices;
        PageLink pageLink = new PageLink(100);
        List<Device> data = new ArrayList<>();

        //获取焊机设备资产信息
        Optional<Asset> asset = client.getAssetById(AssetId.fromString("794e07e0-9250-11ee-ab78-1d9424bc91a7"));

        //获取该资产下的焊机设备
        List<EntityRelation> entityRelationList =  client.findByFrom(asset.get().getId(), "Contains",RelationTypeGroup.COMMON);
        System.out.println(entityRelationList);


        List<String> keys = new ArrayList<>();
        keys.add("A相电流");
        keys.add("AB线电压");
        keys.add("运行时间");
        keys.add("运行时间24");

        //获取最新数据
        for (EntityRelation item : entityRelationList){
//            item.getTo().getEntityType()
            UUID deviceId =   item.getTo().getId();
            EntityId entityId = new DeviceId(deviceId);

            List<TsKvEntry> tsKvEntries = new ArrayList<>();
            try{
                tsKvEntries = client.getLatestTimeseries(entityId,keys);
                System.out.println(tsKvEntries);
            }catch(Exception ex){
                System.out.println(ex);
            }
        }

//        client.logout();
//        client.close();
    }

    public void getDevicesHistoryData(UUID id){
        List<String> keys = new ArrayList<>();
        keys.add("A相电流");
        keys.add("AB线电压");
        keys.add("运行时间");
        keys.add("运行时间24");
        keys.add("作业率");
        keys.add("利用率");
//        keys.add("设备号");
        keys.add("设备名称");
        keys.add("设备状态");
        keys.add("设备编号");
//        keys.add("温度");
        keys.add("开机率");

        EntityId entityId = new DeviceId(id);

        List<TsKvEntry> tsKvEntries = client.getTimeseries(entityId,keys,null,null,null,1718787256000L,1721379256000L,null,false);
        System.out.println("#####################################################");
//        for (TsKvEntry entry : tsKvEntries) {
//            System.out.println(entry);
//        }
        System.out.println(tsKvEntries);
    }

    public void getDevicesHistoryDataListId(ArrayList<UUID> idList) throws IOException {
        List<String> keys = new ArrayList<>();
        keys.add("A相电流");
        keys.add("AB线电压");
        keys.add("运行时间");
        keys.add("运行时间24");
        keys.add("开机率");
        keys.add("作业率");
        keys.add("作业率2");
        keys.add("利用率");
        keys.add("设备名称");
        keys.add("设备状态");
        //不一定全有
        keys.add("设备编号");
        keys.add("资产编号");
        String file_path_floder="db/";
        EntityId entityId=null;
        List<TsKvEntry> tsKvEntries=null;
        for(UUID id:idList){
            entityId = new DeviceId(id);
            tsKvEntries = client.getTimeseries(entityId,keys,null,null,null,1718787256000L,1721379256000L,null,false);
            System.out.println(id);
            System.out.println(tsKvEntries);
            BufferedWriter bw = new BufferedWriter(new FileWriter(file_path_floder+id.toString()+".csv"));

            for(TsKvEntry entry: tsKvEntries){
                    System.out.println(entry);
                    bw.write(entry.toString()+"\n");
            }

            /**
             * save the key-value to csv file
             */
            bw.close();
        }


    }
    public  static void main(String []args) throws IOException {
//        BufferedReader reader = new BufferedReader(new FileReader("db/st.csv"));
//        String line = null;
//        ArrayList<UUID> u_Id=new ArrayList<>();
//        while((line = reader.readLine())!= null) {
////            line = reader.readLine();
//            u_Id.add(UUID.fromString(line));
//            System.out.println(line); //第一行
//        }
//        reader.close();

//        CsvReader csvReader = new CsvReader("db/id_list.csv", '\n', Charset.defaultCharset());
//        String [] id=null;
//        ArrayList<UUID> u_Id=new ArrayList<>();
//
//        if(csvReader.readRecord()){
//            //读取一行数据
//            String[] vs = csvReader.getValues();
//            for (String v:vs) {
//                System.out.println(v);
//            }
//        }

//        for(String iteme: id){
//            System.out.println(id);
//            u_Id.add(UUID.fromString(iteme));
//        }



        TbClient tb=new TbClient();
        tb.login();
        tb.getDevicesLatestData();
    }
}
